package com.fenghongzhang.day002;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

public class Main3Activity extends AppCompatActivity {
    private Button pop;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);

        pop = (Button) findViewById(R.id.pop);

    }

    public void pop(View view) {

        final PopupWindow popupWindow = new PopupWindow(this);

        //3要求
        popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);


        //当前窗口的属性
        WindowManager.LayoutParams attributes = getWindow().getAttributes();
        attributes.alpha = 0.5f;
        getWindow().setAttributes(attributes);


        //监听win消失
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams attributes = getWindow().getAttributes();
                attributes.alpha = 1f;
                getWindow().setAttributes(attributes);
            }
        });

        View inflate = LayoutInflater.from(this).inflate(R.layout.popwin_layout, null);
        TextView textView = inflate.findViewById(R.id.select);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(Main3Activity.this, "呵呵", Toast.LENGTH_SHORT).show();
                popupWindow.dismiss();
            }
        });
        popupWindow.setContentView(inflate);

        //点击外部取消
        popupWindow.setOutsideTouchable(true);

//        popupWindow.showAsDropDown(pop,100,100);

        //设置动画样式
        popupWindow.setAnimationStyle(R.style.pop);

        View inflate1 = LayoutInflater.from(this).inflate(R.layout.activity_main3, null);
        //显示位置的区别
        popupWindow.showAtLocation(inflate1, Gravity.CENTER,0,0);

    }
}
